package top.lixunda.ecommerce.server.user.api.entity.dto;

import lombok.Data;
import lombok.EqualsAndHashCode;
import top.lixunda.ecommerce.server.user.api.entity.db.UserAddressDO;
import top.lixunda.common.validate.annotation.XumDefault;
import top.lixunda.common.validate.annotation.XumIncludes;
import top.lixunda.common.validate.annotation.XumMax;
import top.lixunda.common.validate.annotation.XumNotNull;

import java.time.LocalDateTime;

/**
 * @author Xumda
 * @version time:2019/12/14 22:13
 * <p>
 * 插入用户地址信息时的DTO
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class UserAddressInsertDTO {

    @XumNotNull
    private Integer provinceId;

    @XumNotNull
    private String name;

    @XumNotNull
    private String provinceName;

    @XumNotNull
    private Long cityId;

    @XumNotNull
    private String cityName;

    @XumNotNull
    private Long countyId;

    @XumNotNull
    private String countyName;

    @XumNotNull
    @XumMax(255)
    private String detail;

    @XumNotNull
    @XumIncludes(numbers = {0, 1})
    private Integer isDefault;

    @XumNotNull
    @XumDefault("")
    private String remark;

    @XumNotNull
    @XumDefault("")
    private String postalCode;

    @XumNotNull
    private String phone;

    @XumNotNull
    private String username;

    public UserAddressDO toUserAddress() {
        UserAddressDO userAddress = new UserAddressDO();
        userAddress.setName(this.name);
        userAddress.setProvinceId(this.provinceId);
        userAddress.setProvinceName(this.provinceName);
        userAddress.setCityId(this.cityId);
        userAddress.setCityName(this.cityName);
        userAddress.setCountyId(this.countyId);
        userAddress.setCountyName(this.countyName);
        userAddress.setDetail(this.detail);
        userAddress.setUsername(this.username);
        userAddress.setPhone(this.phone);
        userAddress.setPostalCode(this.postalCode);
        userAddress.setUseable(UserAddressDO.USEABLE);
        userAddress.setIsDefault(this.isDefault);
        userAddress.setRemark(this.remark);
        LocalDateTime now = LocalDateTime.now();
        userAddress.setTime(now);
        userAddress.setUpdateTime(now);
        return userAddress;
    }
}
